﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Reflection;

namespace Qianwei.Model
{
    /// <summary>
    /// 存放系统全局枚举类型  added by Martin 2011-1-15
    /// </summary>
    public class AppEnum
    {
        #region 通用方法
        public static SortedList GetStatus(System.Type t)
        {
            SortedList list = new SortedList();

            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++) {
                string enumName = a.GetValue(i).ToString();
                int enumKey = (int)System.Enum.Parse(t, enumName);
                string enumDescription = GetDescription(t, enumKey);
                list.Add(enumKey, enumDescription);
            }
            return list;
        }

        public static string GetName(System.Type t, object v)
        {
            try {
                return Enum.GetName(t, v);
            }
            catch (Exception e) {
                return "UNKNOWN";
            }
        }

        public static string GetDescription(System.Type t, object v)
        {
            try {
                FieldInfo fi = t.GetField(GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch (Exception e) {
                return "UNKNOWN";
            }
        }
        #endregion

        #region 新闻类型
        public enum NewType
        {
            [Description("公司新闻")]
            CompanyNew = 1,
            [Description("外部报道")]
            ExterReport = 2,
            [Description("健康知识")]
            HealthKnowledge = 3
        }
        public static SortedList GetNewType()
        {
            return GetStatus(typeof(NewType));
        }
        public static string GetNewType(object v)
        {
            return GetDescription(typeof(NewType), v);
        }
        #endregion

        #region 产品类型
        public enum ProductType
        {
            [Description("月饼")]
            MoonCake = 1,
            [Description("粽子")]
            Dumpling = 2,
            [Description("年货")]
            NewYear = 3,
            [Description("套装")]
            Suit = 4
        }
        public static SortedList GetProductType()
        {
            return GetStatus(typeof(ProductType));
        }
        public static string GetProductType(object v)
        {
            return GetDescription(typeof(ProductType), v);
        }
        #endregion
    }
}
